package com.xea.service;

import java.util.List;

import com.uf.util.PageData;
import com.uf.util.SqlHelper;
import com.xea.model.Channel;
import com.xea.model.Product;

public class ProductService {
	
	/**
	 * 查询所属类别产品
	 * @param name       产品名称
	 * @param channelId   所属类别id
	 * @param topage     第几页
	 * @return
	 */
	public PageData search(String name,int channelId,String topage){
		
		String sql = "select * from Product where channelId="+channelId+" and name like '%"+name+"%' order by sortNo asc";
		return SqlHelper.queryPageForMysql(sql, null, topage, null, Product.class);	
		
	}
	
	/**
	 * 查询所属类别产品
	 * @param name       产品名称
	 * @param channelId   所属类别id
	 * @param topage     第几页
	 * @return
	 */
	public PageData searchout(String name,int channelId,String topage){
		String sql = "select * from Product where pub=1 and channelId in(select id from channel where FIND_IN_SET(id, getChildLst("+channelId+"))) and (name like '%"+name+"%' or pno like '%"+name+"%') order by sortNo asc";
		System.out.println(sql);
		return SqlHelper.queryPageForMysql(sql, null, topage, "9", Product.class);	
		
	}
	
	/**
	 * 查询所属类别产品
	 * @param name       产品名称
	 * @param channelId   所属类别id
	 * @param topage     第几页
	 * @return
	 */
	public String searchout(String name){
		
		String sql = "select id from Channel where name="+name;
		System.out.println(sql);
		List<Channel> list = SqlHelper.query(sql,null, Channel.class);
		if(list == null || list.isEmpty()){
			return "";
		}
		return String.valueOf(list.get(0).getId());
	}
	
	
	/**
	 * 查询所有产品
	 * @param name       产品名称
	 * @param channelId   所属类别id
	 * @param topage     第几页
	 * @return
	 */
	public PageData searchout(String name,String topage){
		
		String sql = "select * from Product where pub=1 and (name like '%"+name+"%' or pno like '%"+name+"%') order by sortNo asc";
		System.out.println(sql);
		return SqlHelper.queryPageForMysql(sql, null, topage, "9", Product.class);	
		
	}
	
	/**
	 * 新增产品
	 * @param Product   产品实体
	 * @return
	 */
	public void add(Product product){
		
		String sql = "insert into Product(pno,channelId,name,material,volume,describes,num,createDate,pub,coverPath) values(?,?,?,?,?,?,?,?,?,?)";
		Object[] params=new Object[]{product.getPno(),product.getChannelId(),product.getName(),product.getMaterial(),
									product.getVolume(),product.getDescribes(),product.getNum(),
									product.getCreateDate(),product.getPub(),product.getCoverPath()};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 所属产品是否存在
	 * @param name  产品名称
	 * @param channelId  所属类别
	 * @return
	 */
	public Product find(String name,int channelId){
		
		String sql = "select * from Product where name = ? and channelId=?";		
		List<Product> list = SqlHelper.query(sql, new Object[]{ name,channelId}, Product.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 所属类别是否还有产品
	 * @param channelId  所属类别
	 * @return
	 */
	public Product find(int channelId){
		
		String sql = "select * from Product where channelId=?";		
		List<Product> list = SqlHelper.query(sql, new Object[]{ channelId}, Product.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 所属产品是否存在,排除传入id
	 * @param name  产品名称
	 * @param channelId  所属类别
	 * @param id  id
	 * @return
	 */
	public Product find(String name,int channelId,int id){
		
		String sql = "select * from Product where name = ? and channelId=? and id!=?";		
		List<Product> list = SqlHelper.query(sql, new Object[]{name,channelId,id}, Product.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	/**
	 * 产品是否存在
	 * @param pno  产品编号 
	 * @return
	 */
	public Product findByPno(String pno){
		
		String sql = "select * from Product where pno = ?";		
		List<Product> list = SqlHelper.query(sql, new Object[]{ pno}, Product.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 产品是否存在(排除传入id)
	 * @param pno  产品编号 
	 * @param id  产品id
	 * @return
	 */
	public Product findByPno(String pno,int id){
		
		String sql = "select * from Product where pno = ? and id!=?";		
		List<Product> list = SqlHelper.query(sql, new Object[]{ pno,id }, Product.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 产品详情
	 * @param id  产品id 
	 * @return
	 */
	public Product findById(int id){
		
		String sql = "select * from Product where id = ?";		
		List<Product> list = SqlHelper.query(sql, new Object[]{ id}, Product.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 删除产品
	 * @param id   产品id
	 * @return
	 */
	public void del(String id){
		
		String sql = "delete from Product where id in("+id+")";
		SqlHelper.excute(sql, null);
		
	}
	
	public void updatenum(int num,int id){
		
		String sql = "update Product set num=? where id=?";
		Object[] params=new Object[]{num,id};
		SqlHelper.excute(sql, params);
		
	}

	
	/**
	 * 修改产品
	 * @param product   产品实体
	 * @return
	 */
	public void update(Product product){
		
		String sql = "update Product set pno=?,name=?,material=?,volume=?,describes=?,pub=?,sortNo=? where id=?";
		Object[] params=new Object[]{product.getPno(),product.getName(),product.getMaterial(),
									product.getVolume(),product.getDescribes(),product.getPub(),
									product.getSortNo(),product.getId()};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 修改首页展示路径
	 * @param product   产品实体
	 * @return
	 */
	public void updatePath(Product product){
		
		String sql = "update Product set coverPath=? where id=?";
		Object[] params=new Object[]{product.getCoverPath(),
									product.getId()};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 修改产品图片数量
	 * @param id   产品id
	 * @param num   产品数量
	 * @return
	 */
	public void modifyPicNum(int id, int num) throws Exception {
		Product p=  this.findById(id);
		this.updatenum(p.getNum()+num, id);
		
	}
	/**
	 * 修改是否公开
	 * @param id   产品id
	 * @param pub   是否公开
	 * @return
	 */
	public void pub(String id,int pub){
		
		String sql = "update Product set pub=? where id in("+id+")";
		Object[] params=new Object[]{pub};
		SqlHelper.excute(sql, params);
		
	}
	
	/**
	 * 查询产品排序序号是否存在
	 * @param sortNo   产品序号id
	 * @return
	 */
	public boolean isSortNo(String sortNo){
		
		String sql = "select * from Product where sortNo = ? and sortNo!=1000";		
		List<Product> list = SqlHelper.query(sql, new Object[]{ sortNo}, Product.class);
		if(list == null || list.isEmpty()){
			return false;
		}
		return true;
		
	}
	
	/**
	 * 查询产品排序序号是否存在
	 * @param sortNo   产品序号id
	 * @param id   产品id
	 * @return
	 */
	public boolean isSortNo(String sortNo,int id){
		
		String sql = "select * from Product where sortNo = ? and id!=? and sortNo!=1000";		
		List<Product> list = SqlHelper.query(sql, new Object[]{ sortNo,id}, Product.class);
		if(list == null || list.isEmpty()){
			return false;
		}
		return true;
		
	}
	
	
}
